package com.rchat.pocmini.common.mvp;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aso.common.dialog.LoadingDialog;
import com.aso.common.http.utils.NetErrCode;
import com.aso.common.utils.TUtil;
import com.aso.common.utils.ToastUtils;
import com.rchat.pocmini.R;

import butterknife.BindString;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by itsdf07 on 2017/8/24.
 */

public abstract class BaseMvpFragment<P extends BasePresenter, M extends BaseModel> extends Fragment
        implements BaseView {
    protected View rootView;
    public P mPresenter;
    public M mModel;

    public Context mContext;
    public Activity mActivity;
    public Fragment mFragment;

    protected Unbinder mUnbinder;

    public LoadingDialog mLoadingDialog;

    @BindString(R.string.tip_net_error)
    String mStringTip_error;
    @BindString(R.string.tip_loading)
    String mTipLoading;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        mContext = mActivity.getApplicationContext();
        mFragment = this;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(getLayoutId(), container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mUnbinder = ButterKnife.bind(this, rootView);

        mPresenter = TUtil.getT(this, 0);

        mModel = TUtil.getT(this, 1);

        if (mPresenter != null) {
            mPresenter.mActivity = mActivity;
        }
        this.initPresenter();
        this.initView();
        this.doAfterInitView();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mPresenter != null) {
            mPresenter.onPResume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mPresenter != null) {
            mPresenter.onPPause();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.onPDestroy();
        }
        mUnbinder.unbind();
    }

/*************************be related to custom methods ********************************/

    /*********************子类实现*****************************/

    /**
     * 简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
     */
    public abstract void initPresenter();

    /**
     * 获取布局文件
     *
     * @return
     */
    public abstract int getLayoutId();

    /**
     * 初始化view
     */
    public abstract void initView();

    public abstract void doAfterInitView();

    /*************************be related to Tip********************************/
    /**
     * 短暂显示Toast提示
     *
     * @param tip
     */
    @Override
    public void showShortToast(String tip) {
        ToastUtils.showToast(getContext(), tip);
    }

    /**
     * 短暂显示Toast提示
     *
     * @param tipId
     */
    @Override
    public void showShortToast(int tipId) {
        showShortToast(getString(tipId));
    }

    /**
     * 显示网络异常toast
     **/
    @Override
    public void showNetError() {
        showNetError(mStringTip_error);
    }

    /**
     * 显示网络异常toast
     *
     * @param errorTip
     */
    @Override
    public void showNetError(String errorTip) {
        showShortToast(errorTip);
    }

    /**
     * 显示网络异常toast
     *
     * @param errCode
     * @param errorTip
     */
    @Override
    public void showNetError(String errCode, String errorTip) {
        if (NetErrCode.ERR_FAILURE.equals(errCode)) {
            showNetError();
        } else {
            showShortToast(errorTip);
        }
    }

    /**
     * 显示网络异常toast
     *
     * @param errorTip
     */
    @Override
    public void showNetError(int errorTip) {
        showShortToast(errorTip);
    }

    @Override
    public void showLoading() {
        showLoading(mTipLoading);
    }

    @Override
    public void showLoading(String content) {
        if (null == mLoadingDialog) {
            if (TextUtils.isEmpty(content)) {
                content = mTipLoading;
            }
            mLoadingDialog = new LoadingDialog(getContext(), content);//getString(R.string.tip_loading)
        }
        mLoadingDialog.show();
    }

    @Override
    public void dismissLoading() {
        if (null != mLoadingDialog) {
            mLoadingDialog.dismiss();
        }
    }

}
